package com.celeste.mcpserverfeishu;

import com.celeste.mcpserverfeishu.config.FeishuConfig;
import com.celeste.mcpserverfeishu.controller.FeishuDocumentController;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.tool.ToolCallbackProvider;
import org.springframework.ai.tool.method.MethodToolCallbackProvider;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

import jakarta.annotation.Resource;

@Slf4j
@SpringBootApplication
public class McpServerFeishuApplication implements CommandLineRunner {

    @Resource
    private FeishuConfig feishuConfig;

    public static void main(String[] args) {
        SpringApplication.run(McpServerFeishuApplication.class, args);
    }
    
    /**
     * 创建ObjectMapper Bean，用于JSON序列化
     * 
     * @return ObjectMapper
     */
    @Bean
    public ObjectMapper objectMapper() {
        ObjectMapper objectMapper = new ObjectMapper();
        // 可选：如果需要驼峰命名转下划线命名
        // objectMapper.setPropertyNamingStrategy(PropertyNamingStrategies.SNAKE_CASE);
        return objectMapper;
    }
    
    /**
     * 创建飞书文档工具回调提供者，使其可以被大模型调用
     * 
     * @param feishuDocumentController 飞书文档控制器
     * @return ToolCallbackProvider
     */
    @Bean
    public ToolCallbackProvider feishuTools(
            FeishuDocumentController feishuDocumentController) {
        return MethodToolCallbackProvider.builder()
                .toolObjects(feishuDocumentController)
                .build();
    }
    
    @Override
    public void run(String... args) throws Exception {
        log.info("检查飞书accessToken配置...");
        if (feishuConfig.getAccessToken() == null) {
            log.warn("飞书accessToken为空，请在application.yaml中设置");
        } else {
            log.info("飞书accessToken已配置: {}", feishuConfig.getAccessToken().substring(0, 10) + "...");
        }
    }
}
